package cn.ds.assets.module.assets.service.metadataModelTechnology;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.time.LocalDateTime;

import cn.ds.assets.module.assets.controller.admin.metadataModelTechnology.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.metadataModelTechnology.BaseMetadataModelTechnologyDO;
import cn.ds.assets.framework.common.pojo.PageResult;

import cn.ds.assets.module.assets.convert.metadataModelTechnology.BaseMetadataModelTechnologyConvert;
import cn.ds.assets.module.assets.dal.mysql.metadataModelTechnology.BaseMetadataModelTechnologyMapper;

import static cn.ds.assets.framework.common.exception.util.ServiceExceptionUtil.exception;
import cn.ds.assets.framework.common.exception.enums.GlobalErrorCodeConstants;
//import static cn.ds.assets.module.assets.enums.ErrorCodeConstants.*;

/**
 * 元数据管理-元模型-技术属性 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class BaseMetadataModelTechnologyServiceImpl implements BaseMetadataModelTechnologyService {

    @Resource
    private BaseMetadataModelTechnologyMapper baseMetadataModelTechnologyMapper;

    @Override
    public Long createBaseMetadataModelTechnology(BaseMetadataModelTechnologyCreateReqVO createReqVO) {
        // 插入
        BaseMetadataModelTechnologyDO baseMetadataModelTechnology = BaseMetadataModelTechnologyConvert.INSTANCE.convert(createReqVO);
        baseMetadataModelTechnologyMapper.insert(baseMetadataModelTechnology);
        // 返回
        return baseMetadataModelTechnology.getId();
    }

    @Override
    public void updateBaseMetadataModelTechnology(BaseMetadataModelTechnologyUpdateReqVO updateReqVO) {
        // 校验存在
        validateBaseMetadataModelTechnologyExists(updateReqVO.getId());
        // 更新
        BaseMetadataModelTechnologyDO updateObj = BaseMetadataModelTechnologyConvert.INSTANCE.convert(updateReqVO);
        baseMetadataModelTechnologyMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseMetadataModelTechnology(Long id) {
        // 校验是否存在
        validateBaseMetadataModelTechnologyExists(id);
        // 逻辑删除
        baseMetadataModelTechnologyMapper.updateToDelete(id, LocalDateTime.now());
    }

    private void validateBaseMetadataModelTechnologyExists(Long id) {
        if (id == null) {
            throw exception(GlobalErrorCodeConstants.ID_NOT_FOUND);
        }
        BaseMetadataModelTechnologyDO dataDo = baseMetadataModelTechnologyMapper.selectById(id);
        if (dataDo == null) {
            throw exception(GlobalErrorCodeConstants.DATA_NOT_FOUND);
        }
    }

    @Override
    public BaseMetadataModelTechnologyDO getBaseMetadataModelTechnology(Long id) {
        return baseMetadataModelTechnologyMapper.selectById(id);
    }

    @Override
    public List<BaseMetadataModelTechnologyDO> getBaseMetadataModelTechnologyList(Collection<Long> ids) {
        return baseMetadataModelTechnologyMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BaseMetadataModelTechnologyDO> getBaseMetadataModelTechnologyPage(BaseMetadataModelTechnologyPageReqVO pageReqVO) {
        return baseMetadataModelTechnologyMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BaseMetadataModelTechnologyDO> getBaseMetadataModelTechnologyList(BaseMetadataModelTechnologyExportReqVO exportReqVO) {
        return baseMetadataModelTechnologyMapper.selectList(exportReqVO);
    }

}
